#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/stat.h>
#include<ncurses.h>
#include<curses.h>
#include<ctype.h>
#define MAX_LINE_LENGTH 1000
#define MAX_BUFFER_SIZE 100000
char buffer[MAX_BUFFER_SIZE];
FILE *Read_file;
char option;
//写入内容到缓冲区
void write_To_Buffer()
{
    char line[MAX_LINE_LENGTH];
    printf("新一行：");
    while (1)
    {
      /*  fgets(line,MAX_LINE_LENGTH,stdin);
        if (strcmp(line,"END\n")==0)
        {
            break;
        }
        strcat(buffer,line);
        */
       fgets(line, MAX_LINE_LENGTH, stdin);
       if (line[0] == 'Q' && line[1] == '\n') {
            break;
        }
        strcat(buffer,line);
    }
    
}
//编辑内容到缓冲区
void edit_Buffer()
{
    printf("%s",buffer);
    printf("格式：行号 新内容：\n");
    int line_Number;
    char newLine[MAX_LINE_LENGTH];
    scanf("%d %[^\n]",&line_Number,newLine);
    //计算行的起始位置
    int offset=0;
    for (int i = 0; i < line_Number-1; i++)
    {
        char *lineEnd=strchr(buffer+offset,'\n');
        if (lineEnd==NULL)
        {
            printf("无效行号\n");
            return;
        }
        offset=lineEnd-buffer+1;
    }
    //替换指定行内容
    char *lineStart=buffer+offset;
    char *lineEnd=strchr(lineStart,'\n');
    if (lineEnd==NULL)
    {
        strcpy(lineStart,newLine);
    }else
    {
        int lineLength=lineEnd-lineStart;
        strncpy(lineStart,newLine,lineLength);
    }
    
}
//保存缓冲区内容到文件
void save_Buffer_To_File()
{
    char fileName[20];
    printf("保存文件名：");
    scanf("%s",fileName);
    Read_file=fopen(fileName,"w");
    if (Read_file==NULL)
    {
        printf("无法保存文件\n");
        return;
    }
    fprintf(Read_file,"%s",buffer);
    fclose(Read_file);
    printf("写入成功！\n");   
}
//检测用户输入的文件名是否与本地的文件名相同
int fileExists(const char *filename)
{
    struct stat buffer;
    return (stat(filename,&buffer)==0);
}
void OpenFile()
{
  char filename[20];
   while (1) {
        printf("请输入文件名（按 Q 退出）：");
        scanf("%s", filename);

        if (strcmp(filename, "Q") == 0 || strcmp(filename, "q") == 0) {
            printf("退出程序。\n");
            break;
        }

        Read_file = fopen(filename, "r");
       if (Read_file!= NULL) {
            printf("成功打开文件 %s。\n", filename);
        } else {
            printf("无法打开文件 %s。\n", filename);
        }
    
    char ch;
    while ((ch=fgetc(Read_file))!=EOF)
    {
        putchar(ch);
    }
    
    fclose(Read_file);
    break;
    }
}
void New_Text_File()
{
    char filename[20];
    while(1)
    {
    printf("请输入文件名（按 Q 退出）：");
    scanf("%s", filename);

    if (strcmp(filename, "Q") == 0 || strcmp(filename, "q") == 0) {
        printf("未创建文件。\n");
        return;
    }

    Read_file = fopen(filename, "w");
    if (Read_file == NULL) {
        printf("无法创建文件 %s。\n", filename);
        return;
    } else {
        printf("文件 %s 创建成功。\n", filename);
     break;
    }

    }
    write_To_Buffer();
    printf("是否保存文件内容？");
    scanf("%c",&option);
    if (option=='Y'||option=='y')
    {
        save_Buffer_To_File();
    }else
    {
        printf("文件取消保存\n");
    }
       
}
void mHelp()
{
    
}
void mininode_index()
{
    printf("O 文件\t N 新建文本文件\t H帮助\n");
    while (1)
    {
        scanf("%c",&option);
        switch (option)
        {
        case 'O':
            OpenFile();
            break;
        case 'N':
        New_Text_File();
        break;
        case 'H':
        mHelp();
        break;
        case 'Q':
        break;
        default:
        printf("无效选择\n");
            break;
        }
        return;
    }
    
}
void mininode_home()
{
    while(1)
    {
        printf("W:写入\n");
        printf("E:编辑\n");
        printf("S:保存\n");
        printf("Q:退出\n");
        scanf("%c",&option);
        switch (option)
        {
         case 'w':
            write_To_Buffer();
            break;
         case 'E':
         edit_Buffer();
         break;
         case 'S':
         save_Buffer_To_File();
         break;
         case 'Q':
         return;
         break;
         default:
         printf("无效选择，请重新输入：\n");
            break;
        }
    }
}
int main()
{
    mininode_index();
}